#!/bin/bash
# updatux is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
# Jon Peeters, July 2011

_successfully_updated_en="The new version is successfully installed\nYou should restart the program"
_not_successfully_updated_en="The new version is not successfully installed\n\n"
_successfully_updated_fr="La nouvelle version est correctement installée\nVeuillez redémarrer le programme"
_not_successfully_updated_fr="La nouvelle version n'est pas correctement installée\n\n"
_install_new_version_en="A new version is available\nDo you want to install it?"
_install_new_version_fr="Une nouvelle version est disponible\nDésirez-vous l'installer?"
repository="http://linappux.googlecode.com/hg"
dir="$1"
language="${LANG:0:2}"

if [[ -z "$language" ]] || ([[ "$language" != "en" ]] && [[ "$language" != "fr" ]]); then # No specific language
	language="en"
fi
_successfully_updated_="_successfully_updated_$language"
_not_successfully_updated_="_not_successfully_updated_$language"
_install_new_version_="_install_new_version_$language"
clear
if [[ -z "$dir" ]]; then	
	error="No program to update => exit"
	zenity --error --text="${!_not_successfully_updated_}($error)"
	exit 1
fi
if [[ ! -d "$dir" ]]; then
	error="No directory => exit"
	zenity --error --text="${!_not_successfully_updated_}($error)"
	exit 1
fi 
if [[ ! -f "$dir/version" ]]; then
	error="No version => exit"
	zenity --error --text="${!_not_successfully_updated_}($error)"
	exit 1
fi
vlocal=$(cat "$dir/version" |egrep -o "^[0-9]{6}-[0-9]{4}$")
if [[ -z "$vlocal" ]]; then
	error="No local version => exit"
	zenity --error --text="${!_not_successfully_updated_}($error)"
	exit 1
fi
vremotefile=$(mktemp)
remotedir="$repository/$(basename $dir)"
wget "$remotedir/version" -O "$vremotefile"
if [[ $? -ne 0 ]]; then
	error="No remote version => exit"
	zenity --error --text="${!_not_successfully_updated_}($error)"
	exit 1
fi
vremote=$(cat "$vremotefile" |egrep -o "^[0-9]{6}-[0-9]{4}$")
if [[ -z "$vremote" ]]; then
	error="No remote version => exit"
	zenity --error --text="${!_not_successfully_updated_}($error)"
	exit 1
fi
rm "$vremotefile"
if [[ "$vlocal" < "$vremote" ]]; then
	zenity --question --text="${!_install_new_version_}"
	if [[ $? -eq 0 ]]; then
		wget -nH --cut-dirs=2 -r --no-parent -R "index.html" "$remotedir/" -P "$dir"
		if [[ $? -eq 0 ]]; then
			zenity --info --text="${!_successfully_updated_}"
		else
			error="Cannot download the new version"
			zenity --error --text="${!_not_successfully_updated_}($error)"
			exit 1
		fi
	fi
fi
